import {
	getDics,
	getICTypes,
	getSystemRoles,
	getProjects,
	satisfactionResultOptions,
	evaluationDimensionOptions
} from '@/api/api.js'
import qs from "qs";
import {
	toast
} from './utils.js'

import envParams from '@/api/env.js'

let commonData = {
	getUserInfo: function(){
		return uni.getStorageSync('userInfo') || {}
	},
	lawyerStatus: function(icType){
		let lawyerStatus = uni.getStorageSync('lawyerStatus') || {}
		console.log('lawyerStatus', lawyerStatus)
		return lawyerStatus[icType] || false
	},
	imUserId: function() {
		return envParams.VUE_APP_IM_SYSTEM_USER_PRE + this.getUserInfo().userAccount
	},
	hasRole: async function(role){
		let systemRoles = await this.getSystemRoles()
		return systemRoles.indexOf(role) >= 0
	},
	isAdmin: function(){
		return this.getUserInfo().isAdmin || false
	},
	getSystemRoles: async function(isForce = false) {
		let systemRoles = uni.getStorageSync("systemRoles") || []
		if (systemRoles.length == 0 || isForce) {
			await getSystemRoles(null).then(res => {
				if (res.success) {
					systemRoles = res.result || []
					uni.setStorageSync("systemRoles", systemRoles)
				} else {
					toast(res.message)
				}
			})
		}
		return systemRoles;
	},
	getICTypes: async function(isForce = false) {
		let icTypes = uni.getStorageSync("icTypes") || []
		if (icTypes.length == 0 || isForce) {
			await getICTypes(null).then(res => {
				if (res.success) {
					icTypes = res.result || []
					uni.setStorageSync("icTypes", icTypes)
				} else {
					toast(res.message)
				}
			})
		}
		return icTypes
	},
	getDics: async function(isForce = false, params) {
		let dics = uni.getStorageSync("dic_" + params.category) || []
		if (dics.length == 0 || isForce) {
			await getDics(params).then(res => {
				if (res.success) {
					dics = res.result || []
					uni.setStorageSync("dic_" + params.category, dics)
				} else {
					toast(res.message)
				}
			})
		}
		return dics
	},
	getProjects: async function(isForce = false, params) {
		let projects = uni.getStorageSync("projects_" + qs.parse(params)) || []
		if (projects.length == 0 || isForce) {
			await getProjects(params).then(res => {
				if (res.success) {
					projects = res.result || []
					uni.setStorageSync("projects_" + qs.parse(params), projects)
				} else {
					toast(res.message)
				}
			})
		}
		return projects
	},
	getEvaluationDimensionOptions: async function(isForce = false) {
		let options = uni.getStorageSync("evaluationDimensionOptions") || []
		if (options.length == 0 || isForce) {
			await evaluationDimensionOptions().then(res => {
				if (res.success) {
					options = res.result || []
					uni.setStorageSync("evaluationDimensionOptions", options)
				} else {
					toast(res.message)
				}
			})
		}
		return options
	},
	getSatisfactionResultOptions: async function(isForce = false) {
		let options = uni.getStorageSync("satisfactionResultOptions") || []
		console.log('getSatisfactionResultOptions')
		if (options.length == 0 || isForce) {
			await satisfactionResultOptions().then(res => {
				if (res.success) {
					options = res.result || []
					uni.setStorageSync("satisfactionResultOptions", options)
				} else {
					toast(res.message)
				}
			})
		}
		return options
	}
}
export default commonData